/**
 * OPay Inc.
 * Copyright (c) 2016-2022 All Rights Reserved.
 */
package cc.dd.marketing.coupon.rule.checker;

import cc.dd.marketing.rule.base.RuleChecker;
import cc.dd.marketing.system.exception.base.BusinessException;
import cc.dd.marketing.system.exception.response.ResultCodeEnum;
import cc.dd.marketing.coupon.dto.CouponUseInnerReqDTO;
import cc.dd.marketing.coupon.rule.domain.CouponAmountLimitRule;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @Auther: dongsufeng
 * @Date: 2022/9/23 12:04
 * @Description:
 */
@Component
@Slf4j
public class CouponAmountLimitRuleChecker implements RuleChecker<CouponAmountLimitRule, CouponUseInnerReqDTO> {

    @Override
    public Boolean validate(CouponAmountLimitRule rule, CouponUseInnerReqDTO request) {
        String json = rule.getRule();
        JSONObject jsonObject = JSON.parseObject(json);
        BigDecimal amountLimit = jsonObject.getBigDecimal("amountLimit");
        if (amountLimit == null || amountLimit.compareTo(request.getOrderAmount()) > 0) {
            log.info("优惠券使用金额限制规则rule:{} param:{}",amountLimit,request.getOrderAmount());
            throw new BusinessException(ResultCodeEnum.RULE_CHECK_FAIL.getCode(), "交易金额不满足优惠券使用金额规则");
        }
        return true;
    }

}
